package com.example.erp.service.permission.impl;

import cn.hutool.core.util.StrUtil;
import com.example.erp.config.CommonConfig;
import com.example.erp.dto.permissionDTO.RoleDTO;
import com.example.erp.mapper.permission.RoleMapper;
import com.example.erp.pojo.permission.Role;
import com.example.erp.service.permission.RoleService;
import com.example.erp.utils.DataFormatUtils;
import com.example.erp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceA implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result list(RoleDTO roleDTO) {
        roleDTO.setOffset((roleDTO.getPageNum() - 1) * roleDTO.getPageSize());
        List<Role> list = roleMapper.list(roleDTO);
        list.forEach(it -> it.setMenuIds(
                StrUtil.isNotEmpty(it.getMenuIdsStr())
                        ? DataFormatUtils.getListOfStr(it.getMenuIdsStr(),Long.class)
                        : new ArrayList<>()
        ));
        Integer count = roleMapper.count(roleDTO);
        return Result.successMsg(Map.of(
                "rows", list,
                "total", count
        ));
    }

    @Override
    public Result listAll() {
        List<Role> list = roleMapper.listAll();
        return Result.success(list);
    }

    @Override
    public Result add(Role role) {
        if(roleMapper.getByName(role.getRoleName()) != null){
            return Result.error(CommonConfig.resultMsg.ROLENAMEQUERY.getDescription());
        }
        String menuIdsStr = DataFormatUtils.getStrOfList(role.getMenuIds());
        role.setMenuIdsStr(menuIdsStr);
        Integer count = roleMapper.add(role);
        if (count != null && count >= 1) {
            return Result.successMsg(role.getId(),CommonConfig.msgTYPE.ADD.getDescription());
        } else {
            return Result.error(CommonConfig.msgTYPE.ADDERR.getDescription());
        }
    }

    @Override
    public Result edit(Role role) {
        if(roleMapper.getByIdName(role.getId(),role.getRoleName()) != null){
            return Result.error(CommonConfig.resultMsg.ROLENAMEQUERY.getDescription());
        }
        String menuIdsStr = DataFormatUtils.getStrOfList(role.getMenuIds());
        role.setMenuIdsStr(menuIdsStr);
        Integer count = roleMapper.edit(role);
        if (count != null && count == 1) {
            return Result.successMsg(CommonConfig.msgTYPE.EDIT.getDescription());
        } else {
            return Result.error(CommonConfig.msgTYPE.EDITERROR.getDescription());
        }
    }

    @Override
    public Result remove(List<Long> ids) {
        Integer count = roleMapper.remove(ids);
        if (count != null && count  >= 1) {
            return Result.successMsg(CommonConfig.msgTYPE.REMOVE.getDescription());
        } else {
            return Result.error(CommonConfig.msgTYPE.REMOVEERROR.getDescription());
        }
    }
}
